class SingleNode(object):
    def __init__(self,item):
        self.item = item
        self.next = None

class SingleList(object):
    def __init__(self):
        self.head = None
    def isempty(self):
        return self == None
    def length(self):
        cur = self.head
        count = 0
        while cur != None:
            count += 1
            cur = cur.next
        return count
    def travel(self):
        cur = self.head
        while cur != None:
            print(cur.item)
            cur = cur.next
        print("")
    def add(self,item):
        node = SingleNode(item)
        node.next = self.head
        self.head = node
    def append(self,item):
        node = SingleNode(item)
        if self.isempty():
            self.head = node
        else:
            cur = self.head
            while cur.next != None:
                cur = cur.next
            cur.next = node
    def insert(self,pos,item):
        node = SingleNode(item)
        if pos <= 0:
            self.add(item)
        elif pos > (self.length() -1):
            self.append(item)
        else:
            count = 0
            pre = self.head
            while count < (pos -1):
                pre = pre.next
                count += 1
            node.next = pre.next
            pre.next = node
    def remove(self,item):
        cur = self.head
        pre = None
        while cur != None:
            if cur.item == item:
                if pre == None:
                    self.head = cur.next
                else:
                    pre.next = cur.next
                break
            else:
                pre = cur
                cur = cur.next
    def search(self,item):
        cur = self.head
        while cur != None:
            if cur.item == item:
                return True
            cur = cur.next
        return False

if __name__ == "__main__":
    list1 = SingleList()
    list1.add(2)
    list1.add(3)
    list1.travel()
    list1.append(5)
    list1.insert(3,5)
    list1.travel()
    print("-"*33)
    list1.remove(2)
    list1.travel()
    print("长度%d"%list1.length())